package com.augurit.wf.client.remind.web;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.action.BaseAction;
import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.base.extmodel.BasicCombo;
import com.augurit.common.base.extmodel.ExtResult;
import com.augurit.common.util.ExtUtils;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.wf.client.remind.service.IWfRemindService;
import com.augurit.wf.client.remind.web.form.WfRemindForm;

@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "wf-remind.action", type = "redirect"),
	@Result(name = CrudActionSupport.SUCCESS, location = "/remind/WfRemind/WfRemind.jsp"),
	@Result(name = CrudActionSupport.INPUT, location = "/remind/WfRemind/WfRemind-input.jsp"),
	@Result(name = WfRemindAction.REMIND_WIDGET, location = "/wf/client/remind/remind-widget.jsp")
})
public class WfRemindAction extends CrudActionSupport<WfRemindForm>{

	private final static String REMIND_WIDGET = "remind-widget";
	
	@Autowired
	private IWfRemindService wfRemindService;
	
	private WfRemindForm form;
	private Long id;
	private Long[] checkedIds;
	private Page<WfRemindForm> page = new Page<WfRemindForm>(10);


	/**
	 * 分页查询
	 */
	@Override
	public String list() throws Exception {
		ExtUtils.initPageFromExtGridParam(BaseAction.getRequest(), page);
		wfRemindService.search(page, form);
		ExtBaseAction.renderText(Struts2Utils.getJsonString(page, DateUtils.YYYY_MM_DD_HH_MM_SS));
		return null;
	}
	
	public String getRemindWidget() throws Exception {
		return REMIND_WIDGET;
	}

	/**
	 * 新增或修改
	 */
	@Override
	public String input() throws Exception {
		List list = new ArrayList();
		list.add(form);
		
		ExtResult result = new ExtResult();
		result.setResult(list);
		result.setTotalCount(1);
		result.setSuccess(true);
		BaseAction.renderJson(Struts2Utils.getJsonString(result, DateUtils.YYYY_MM_DD_HH_MM_SS));
		return null;
	}

	/**
	 * 保存新增或者修改的Form对象
	 */
	@Override
	public String save() throws Exception {
		wfRemindService.saveRemind(form);
		ExtBaseAction.extRenderSuccess();
		return null;
	}

	/**
	 * 删除一条记录
	 */
	@Override
	public String delete() throws Exception {
		wfRemindService.delete(checkedIds);
		return null;
	}
	
	/**
	 * 删除多条记录
	 */
	@Override
	public String deleteMore() throws Exception{
		wfRemindService.delete(checkedIds);
		return RELOAD;
	}
	
	/**
	 * 准备页面模型
	 */
	@Override
	protected void prepareModel() throws Exception {
		if(id != null){
			form = wfRemindService.get(id);
		}
		else
			form = new WfRemindForm();
	}
	
	public WfRemindForm getModel() {
		return form;
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getReminder() throws Exception {
		List<BasicCombo> combos = new ArrayList<BasicCombo>();
		combos.add(BasicCombo.getBlankItem());
		
		Map map = wfRemindService.getReminder(form.getWfInstanceId());
		if(map != null){
			Iterator iter = map.entrySet().iterator();
			while(iter.hasNext()){
				Map.Entry<String, String> entry = (Map.Entry<String, String>)iter.next();
				BasicCombo combo = new BasicCombo();
				combo.setLabel(entry.getValue());
				combo.setValue(entry.getKey());
				combos.add(combo);
			}
		}
		
		ExtBaseAction.extRenderListJson(combos, true);
		return null;
	}

	
	
	// 属性getter和setter方法
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long[] getCheckedIds() {
		return checkedIds;
	}

	public void setCheckedIds(Long[] checkedIds) {
		this.checkedIds = checkedIds;
	}
	
	public Page<WfRemindForm> getPage() {
		return page;
	}


	public void setPage(Page<WfRemindForm> page) {
		this.page = page;
	}
}
